module GoalsHelper
  def display_progress(goal, width = 200)
    html = ""
    html << %Q(<table class="progress-bar" style="width: #{width}px;" cellspacing="0" cellpadding="0"><tr>)
    html << %Q(<td class="failure activatable" style="width: #{goal.progress_percentages.failure}%" title="#{goal.progress.failure} of #{pluralize(goal.progress.total, "checkpoint")} failed."></td>)
    html << %Q(<td class="pending activatable" style="width: #{goal.progress_percentages.pending}%" title="#{goal.progress.pending} of #{pluralize(goal.progress.total, "checkpoint")} pending."></td>)
    html << %Q(<td class="success activatable" style="width: #{goal.progress_percentages.success}%" title="#{goal.progress.success} of #{pluralize(goal.progress.total, "checkpoint")} succeeded."></td>)
    html << %Q(</tr></table>)
    
    html
  end
  
  def display_checkpoint(checkpoint, admin = false)
    state_text = ["Pending", "Sucess", "Failure"]
    state_class = ["pending", "success", "failure"]
    
    html = ""
    html << %Q( <tr id="goal_checkpoint_#{checkpoint.id}"> )
    html << %Q(   <td class="targetted">#{checkpoint.targetted_at.strftime("%Y.%m.%d")}</td> )
    html << %Q(   <td class="state_color #{state_class[checkpoint.state.to_i]}"></td> )
    html << %Q(   <td class="state_text">#{state_text[checkpoint.state.to_i]}</td> )
    if admin
      states  = [ ["Pending", GoalCheckpoint::STATE_PENDING], 
                  ["Success", GoalCheckpoint::STATE_SUCCESS], 
                  ["Failure", GoalCheckpoint::STATE_FAILURE] ]
      current = states[checkpoint.state.to_i]
      
      html << %Q(   <td class="state_select"> )
      html << 
        select_tag(
          "checkpoint_state", 
          options_for_select(states, current),
          :class    => "default",
          :onchange => "Goals.changeState(#{checkpoint.id}, this.value); return false;"
        )
      html << %Q(   </td> )
    end
    
    html << %Q( </tr> )
  end
end
